package booksystem.dao.impl;


import booksystem.dao.BaseDao;
import booksystem.dao.OrderDao;
import booksystem.entity.Order;

import java.util.List;

public class OrderDaoImpl extends BaseDao<Order> implements OrderDao {
    @Override
    public int save(Order order) {
        // 保存用户订单
        String sql = "insert into `order`(order_id, book_id, user_id, buy_num, price, statue, create_time) value(?,?,?,?,?,?,?)";
        // 购买数量、价格和状态通过servlet计算得出
        return update(sql, order.getOrder_id(), order.getBook_id(), order.getUser_id(), order.getBuy_num(), order.getPrice(), order.getStatue(), order.getCreate_time());
    }

    @Override
    public List<Order> getCurrentUserOrder(String id) {
        // 根据用户id获取用户订单
        String sql = "select * from `order` where user_id=? order by create_time desc";
        return getBeanList(Order.class, sql, id);
    }

    @Override
    public List<Order> getAllOrder() {
        // 获取所有订单
        String sql = "select * from `order` order by order_id desc";
        return getBeanList(Order.class, sql);
    }

    @Override
    public int deleteOrder(String id) {
        // 根据 order_id 删除订单
        String sql = "delete from `order` where order_id=?";
        return update(sql, id);
    }
}
